<?php
/*
Plugin Name: 聚合数据
Description: 聚合数据
Plugin URI: http://blog.wpjam.com/
Author URI: http://blog.wpjam.com/
Version: 0.1
*/

function wpjam_juhe_remote_request($url, $args=array()){	
	return wpjam_remote_request($url, $args, array('errcode'=>'error_code','errmsg'=>'reason'));
}

// 万年历
function wpjam_get_juhe_calendar($date, $key='d53894cae955477b6d9a63890ecd36da'){
	$juhe_calendar = get_transient('juhe_calendar_'.$date);
	if($juhe_calendar === false){
		$url = 'http://japi.juhe.cn/calendar/day?date='.$date.'&key='.$key;
		$response = wpjam_juhe_remote_request($url);
		if(is_wp_error($response)){
			return $response;
		}
		$juhe_calendar = $response['result']['data'];
		set_transient('juhe_calendar_'.$date, $juhe_calendar, DAY_IN_SECONDS);
	}
	return $juhe_calendar;
}

// 星座
function wpjam_get_juhe_constellation($constellation, $key='e0c1a7c0408e40a85aebb06f6c23c7f5'){
	$today	= date('Y-m-d', current_time('timestamp'));
	$juhe_constellation = get_transient('juhe_constellation_'.$today.'_'.$constellation);

	if($juhe_constellation === false){
		$url = 'http://web.juhe.cn:8080/constellation/getAll?consName='.urlencode($constellation).'&type=today&key='.$key;
		$response = wpjam_juhe_remote_request($url);
		if(is_wp_error($response)){
			return $response;
		}
		$juhe_constellation = $response;
		set_transient('juhe_constellation_'.$today.'_'.$constellation, $response, DAY_IN_SECONDS);
	}
	return $juhe_constellation;
}